/* 基于数组的队列 */

#include "queue_base_array.h"

/* 创建 */
QUEUE* queue_create (int cap) 
{
	QUEUE* queue = malloc (sizeof (QUEUE));
	queue->arr   = malloc (cap * sizeof (int));
	queue->cap   = cap;
	queue->front = 0;
	queue->back  = 0;
	queue->size  = 0;
	return queue;
}

/* 销毁 */
void queue_destroy (QUEUE* queue) 
{
	free (queue->arr);
	free (queue);
}

/* 压入 */
void queue_push (QUEUE* queue, int data) 
{
	if (queue->back >= queue->cap)
        {
		queue->back = 0;
        }

	(queue->size)++;

	queue->arr[queue->back++] = data;
}

/* 弹出 */
int queue_pop (QUEUE* queue) 
{
	if (queue->front >= queue->cap)
        {
		queue->front = 0;
        }

	(queue->size)--;

	return queue->arr[queue->front++];
}


/* 清空 */
void queue_clear (QUEUE* queue) 
{

	queue->front = 0;
	queue->back  = 0;
	queue->size  = 0;
}

/* 大小 */
int queue_size (QUEUE* queue) 
{
	return queue->size;
}

/* 判满 */
int queue_full (QUEUE* queue) 
{
	if(queue->size >= queue->cap)
        {
            return 1;
        }

        return 0;
}

/* 判空 */
int queue_empty (QUEUE* queue) 
{
	if(queue->size)
        {
            return 0;
        }

        return 1;
}




